using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;

namespace CRMDocumentation
{
    public partial class ThisAddIn
    {
        private Ribbon ribbon;
    
        protected override object RequestService(Guid serviceGuid)
        {
            if (serviceGuid == typeof(Office.IRibbonExtensibility).GUID)
            {
                if (ribbon == null)
                    ribbon = new Ribbon();
                return ribbon;
            }
    
            return base.RequestService(serviceGuid);
        }
    }

    [ComVisible(true)]
    public class Ribbon : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;
        
        public Ribbon()
        {
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("CRMDocumentation.Ribbon.xml");
        }
        private ExcelInteractionHelper xih;

        #endregion

        #region Ribbon Callbacks

        public void OnLoad(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
            xih = new ExcelInteractionHelper();
        }

        public void OnToggleButton1(Office.IRibbonControl control, bool isPressed)
        {
            if (isPressed)
                MessageBox.Show("Pressed");
            else
                MessageBox.Show("Released");
        }

        internal class PictureDispMaker : AxHost
        {
            private PictureDispMaker() : base("") { }

            static public stdole.IPictureDisp ConvertImage(System.Drawing.Image image)
            {
                return (stdole.IPictureDisp)GetIPictureDispFromPicture(image);
            }

            static public stdole.IPictureDisp ConvertIcon(System.Drawing.Icon icon)
            {
                return ConvertImage(icon.ToBitmap());
            }
        }

        public stdole.IPictureDisp GetImage(Office.IRibbonControl control)
        {
            stdole.IPictureDisp image = null;

            switch (control.Id)
            {
                case "textButtonLoadXml":
                    image = PictureDispMaker.ConvertIcon(Properties.Resources.crm_logo);
                    break;
            }
            return image;
        }

        public void OnTextButtonLoadXml(Office.IRibbonControl control)
        {
            try
            {
                xih.LoadXml();
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred " + ex.Message);
            }
        }

        public void OnTextButtonScripts(Office.IRibbonControl control)
        {
            try
            {
                xih.DocScripts(Globals.ThisAddIn.Application);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred " + ex.Message);
            }
        }

        public void OnTextButtonForms(Office.IRibbonControl control)
        {
            try
            {
                xih.DocForms(Globals.ThisAddIn.Application);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred " + ex.Message);
            }

        }

        public void OnTextButtonPicklists(Office.IRibbonControl control)
        {
            try
            {
                xih.DocPicklists(Globals.ThisAddIn.Application);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred " + ex.Message);
            }
        }

        #endregion

        #region Helpers

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }

        #endregion
    }
}
